package com.Q01;

/**
 * @version v1.0.0
 * 创建时间：2022/6/20 17:28
 */
public class Q01_05 {
    public boolean oneEditAway(String first, String second) {
        // 分类讨论
        if (Math.abs(first.length()-second.length())>=2){
            return false;
        }
        if (first.length() == second.length()){
            boolean flag = false;
            for(int i = 0 ;i<first.length();i++){
                if (first.charAt(i) != second.charAt(i) && !flag){
                    flag = true;
                    continue;
                }
                if (first.charAt(i) != second.charAt(i) && flag){
                    return false;
                }
            }
            return true;
        }

        // 除去最多一次编辑后，前缀和和后缀应该相同
        String longer = "";
        String shorter = "";
        if (first.length() > second.length()){
            longer = first;
            shorter = second;
        }else {
            longer = second;
            shorter = first;
        }

        int lp = 0;
        int sp = 0;
        while(lp!= longer.length()&&sp!=shorter.length()){
            if (longer.charAt(lp) == shorter.charAt(sp)){
                lp++;
                sp++;
            }else
                lp++;
        }
        return lp - sp<=1;
    }

}
